package com.zat.mes.andrmes.dofun.qualitytest.adapter

import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.QcRecordHasBean
import com.zat.mes.andrmes.dofun.qualitytest.DealOnSiteHasActivity
import com.zat.mes.andrmes.dofun.qualitytest.OnSiteHasActivity
import com.zat.mes.andrmes.global.CompanionType.Companion.INTENT_EXTRA_QCID
import com.zat.mes.andrmes.utils.SpannableStrUtil
import kotlinx.android.synthetic.main.item_item_quality_plan.view.*

class QualityRecordAdapter(layoutResId: Int) :
    BaseQuickAdapter<QcRecordHasBean, BaseViewHolder>(layoutResId) {


    override fun convert(helper: BaseViewHolder, item: QcRecordHasBean) {

        helper.setText(R.id.tvNameQualityPlanItem, item?.productName)
        var styleNo = context.getString(R.string.text_style) + " " + item?.styleNo
        var color = context.getString(R.string.text_color) + " " + item?.color
        var size = context.getString(R.string.text_size) + " " + item?.size
        val textColor = ContextCompat.getColor(context, R.color.color_text_grey)
        helper.setText(
            R.id.tvStyleNoQualityPlanItem,
            SpannableStrUtil.getColorSpannable(styleNo, 3, styleNo.length, textColor)
        )
        helper.setText(
            R.id.tvColorQualityPlanItem,
            SpannableStrUtil.getColorSpannable(color, 3, color.length, textColor)
        )
        helper.setText(
            R.id.tvSizeQualityPlanItem,
            SpannableStrUtil.getColorSpannable(size, 3, size.length, textColor)
        )

        val ivHideShow = helper.getView<ImageView>(R.id.ivQualityPlanShowHide)
        val llPro = helper.getView<LinearLayout>(R.id.llProcQualityPlanItem)
        if (item?.workQcRecordList !== null) {
            llPro.removeAllViews()
            for (orderCraftprocess in item?.workQcRecordList) {
                val inflate = LayoutInflater.from(context)
                    .inflate(R.layout.item_item_quality_plan, null)
                inflate.tvProcNameQualityPlanItem.text = orderCraftprocess.procName
                inflate.llNameDate.visibility = View.GONE
                if ("2".equals(orderCraftprocess.status)) {
                    //未处理
                    inflate.tvTypeQualityPlanItem.text = context.getString(R.string.deal_not)
                    inflate.tvTypeQualityPlanItem.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_not_finished
                        )
                    )
                    inflate.tvTypeQualityPlanItem.setBackgroundColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_bg_not_finished
                        )
                    )
                } else if ("3".equals(orderCraftprocess.status)) {
                    inflate.tvTypeQualityPlanItem.text = context.getString(R.string.approve_has)
                    inflate.tvTypeQualityPlanItem.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_has_finished
                        )
                    )
                    inflate.tvTypeQualityPlanItem.setBackgroundColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_bg_has_finished
                        )
                    )
                } else if ("9".equals(orderCraftprocess.status)) {
                    inflate.tvTypeQualityPlanItem.text = context.getString(R.string.has_finished)
                    inflate.tvTypeQualityPlanItem.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_has_finished
                        )
                    )
                    inflate.tvTypeQualityPlanItem.setBackgroundColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_bg_has_finished
                        )
                    )
                }
                llPro.addView(inflate)

                inflate.btnMoreQualityPlanItem.setOnClickListener {
                    if ("2".equals(orderCraftprocess.status)) {
                        //未处理，
                        val intent = Intent(context, DealOnSiteHasActivity::class.java)
                        intent.putExtra(INTENT_EXTRA_QCID, orderCraftprocess?.qcId)
                        startMyActivity(intent)
                    } else if ("3".equals(orderCraftprocess.status)) {
                        //已经处理
                        val intent = Intent(context, OnSiteHasActivity::class.java)
                        intent.putExtra(INTENT_EXTRA_QCID, orderCraftprocess?.qcId)
                        startMyActivity(intent)
                    } else if ("9".equals(orderCraftprocess.status)) {
                        //已完成
                        val intent = Intent(context, OnSiteHasActivity::class.java)
                        intent.putExtra(INTENT_EXTRA_QCID, orderCraftprocess?.qcId)
                        startMyActivity(intent)
                    }
                }
            }
        }


        if (item?.show!!) {
            ivHideShow.setImageResource(R.mipmap.icon_shengchanjihua_heqi)
            llPro.visibility = View.VISIBLE
        } else {
            ivHideShow.setImageResource(R.mipmap.icon_shengchanjihua_zhankai)
            llPro.visibility = View.GONE
        }
        helper.getView<LinearLayout>(R.id.llBtnQualityPlanShowHide).setOnClickListener {
            val layoutPosition = helper.layoutPosition
            Log.e("layoutPosition", layoutPosition.toString())
            //展开收起
            if (item?.show!!) {
                item?.show = false
            } else {
                item?.show = true
            }
            notifyDataSetChanged()
        }
    }

    fun startMyActivity(intent: Intent) {
        try {
            context.startActivity(intent)
        } catch (e: Exception) {
            Log.e("activity", e.toString())
        }
    }
}